package group

import (
	"github.com/gin-gonic/gin"
	_ "image/jpeg"
	_ "image/png"
	"net/http"
	"one-wiki/commons"
	"one-wiki/logic"
	"strconv"
)

func GroupHandlersInit(engine *gin.Engine) {
	// 获取组详细信息
	engine.GET("/group/:id", groupDetail)
	// 从组中踢出用户
	engine.DELETE("/groups/:groupID/members/:userID", moveOutGroup)
}

// 获取组详情信息
func groupDetail(c *gin.Context) {
	// 获取Session 校验Session
	value, exists := c.Get(commons.KEY_SESSION)
	if !exists {
		panic(commons.UnAuthorizationErr)
	}
	localSession, ok := value.(commons.Session)
	if !ok {
		panic(commons.ServerError)
	}

	// 获取组 ID 参数
	groupID, err := strconv.ParseInt(c.Param("id"), 10, 64)
	if err != nil {
		panic(commons.CustomClientError("参数错误"))
	}

	c.JSON(http.StatusOK, logic.GroupDetail(localSession, groupID))
}

// 获取组详情信息
func moveOutGroup(c *gin.Context) {
	// 获取Session 校验Session
	value, exists := c.Get(commons.KEY_SESSION)
	if !exists {
		panic(commons.UnAuthorizationErr)
	}
	localSession, ok := value.(commons.Session)
	if !ok {
		panic(commons.ServerError)
	}

	// 获取组 ID 参数
	groupID, err := strconv.ParseInt(c.Param("groupID"), 10, 64)
	if err != nil {
		panic(commons.CustomClientError("参数错误"))
	}
	// 获取组 ID 参数
	userID, err := strconv.ParseInt(c.Param("userID"), 10, 64)
	if err != nil {
		panic(commons.CustomClientError("参数错误"))
	}

	logic.MoveOutGroup(localSession, groupID, userID)
	c.JSON(http.StatusOK, commons.SuccessResponse)
}
